/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.urchr.todoist.lib;

import java.util.Vector;

/**
 *
 * @author Kostya
 */
public class Task
{
    private int _rmsID;
    private String _parentTaskID="";
    private int _orderAction = -1;
    
    private String id="", projectId="", date="", name="", time="";
    private int priority = 0;
    private boolean checked = false, checkable = false;
    private Vector subTasks = new Vector();
    private int indent = 1;
    private RichNameParser parser = new RichNameParser();
    private Project project = null;
    private Task parentTask = null;
    private boolean collapsed = false;

    public int getRmsID() {
        return this._rmsID;
    }
    public void setRmsID(int rmsID) {
        this._rmsID = rmsID;
    }

    public int getOrderAction() {
        return this._orderAction;
    }
    public void setOrderAction(int orderAction) {
        this._orderAction = orderAction;
    }
    
    public boolean isCollapsed() {
        return collapsed;
    }

    public void setCollapsed(boolean collapsed) {
        this.collapsed = collapsed;
    }
    
    public Task(Project p, Task t){
        project = p;
        parentTask = t;
        
        if (p != null) projectId = p.getId();
        if (t != null) _parentTaskID = t.getId();
    }
    
    
    public void addSubTask(Task t){
        subTasks.addElement(t);
    }
    
    public void removeSubTask(Task t){
        subTasks.removeElement(t);
    }
    
    
    public int getSubTasksCount(){
        return subTasks.size();
    }
    
    public Task getSubTaskAt(int index){
        return (Task) subTasks.elementAt(index);
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getProjectId() {
        return projectId;
    }

    public void setProjectId(String projectId) {
        this.projectId = projectId;
    }

    public int getIndent() {
        return indent;
    }

    public void setIndent(String indent) {
        try {
            this.indent = Integer.parseInt(indent);
        } catch (NumberFormatException numberFormatException) {
            this.indent = 1;
        }
    }

    public void setIndent(int indent) {
        this.indent = indent;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String d) {
        this.date = d;
        if("".equals(this.date))
            this.date = null;
        this.time = null;
        if(this.date!=null){
            if(date.indexOf("@")!=-1){
                time = date.substring(date.indexOf("@")+1).trim();
            }
        }
    }

    public int getPriority() {
        return priority;
    }

    public void setWrongPriority(String pr) {
        setPriority(pr);
        priority = 4-priority+1;
    }
    public void setPriority(String priority) {
        
        try {
            this.priority = Integer.parseInt(priority);

        } catch (NumberFormatException numberFormatException) {
            this.priority = 5;
        }
    }
    public void setPriority(int priority) {
        this.priority = priority;
    }

    public boolean isChecked() {
        return checked;
    }

    public void setChecked(boolean checked) {
        this.checked = checked;
    }

    public void setChecked(String checked) {
        this.checked = "1".equals(checked);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
        parser.parseData(name);
        setCheckable(!parser.isAsterisk());
    }
    
    public String toDashboardString(){
        return "   "+(checked?"* ":(checkable?"* ":"-> "))+
                parser.getClearName()+
                (time!=null?(", "+time):"")+
                (getProject()==null?"":" ("+getProject().getClearName()+")");        
    }

    public String toString() {
        String ws = "";
        for (int i = 0; i < indent-1; i++) {
            ws += "   ";
        }
        return ws+(checked?"- ":(checkable?"* ":"-> "))+(collapsed?"...":"")+
                parser.getClearName()+
                (getDate()!=null?(", "+getDate()):"");
    }

    void printTask(String _ws) {
        String ws = _ws;
        for (int i = 0; i < indent-1; i++) {
            ws += " ";
        }
        System.out.println(ws+toString());
        for (int i = 0; i < getSubTasksCount(); i++) {
            getSubTaskAt(i).printTask(_ws);
        }
    }

    public boolean isCheckable() {
        return checkable;
    }
    
    public String getClearName(){
        return parser.getClearName();
    }
    
    public void setCheckable(boolean checkable) {
        this.checkable = checkable;
    }

    public Project getProject() {
        return project;
    }
    public void setProject(Project p) {
        this.project = p;
        this.projectId = p.getId();
    }

    public Task getParentTask() {
        return parentTask;
    }
    public void setParentTask(Task t) {
        this.parentTask = t;
        this._parentTaskID = t.getId();
    }

    public String getParentTaskID() {
        return _parentTaskID;
    }
    public void setParentTaskID(String parentId) {
        this._parentTaskID = parentId;
    }
    
    public Task searchTask(String id){
        for (int i = 0; i < getSubTasksCount(); i++) {
            Task t = getSubTaskAt(i);
            if(t.getId().equals(id))
                return t;
            t = t.searchTask(id);
            if(t!=null)
                return t;
        }
        return null;
    }
}
